package com.example.vueadmin.controller;

import com.example.vueadmin.entity.Enter;
import com.example.vueadmin.entity.Inventory;
import com.example.vueadmin.entity.InventoryVO;
import com.example.vueadmin.entity.Outer;
import com.example.vueadmin.responseEntity.ServerResponse;
import com.example.vueadmin.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/inventory")
public class InventoryController {
    @Autowired
    private InventoryService inventoryService;

    @PostMapping("/select")
    public ServerResponse selectInventory(@RequestBody InventoryVO inventoryVO) {
        Map<String, Object> res = inventoryService.select(inventoryVO);
        return ServerResponse.ok(res);
    }

    @PostMapping("/enter")
    public ServerResponse enterInventory(@RequestBody Enter enter) {
        Integer res = inventoryService.enter(enter);
        if (res > 0) {
            return ServerResponse.ok("入库成功");
        } else {
            return ServerResponse.badRequest("入库失败");
        }
    }

    @PostMapping("/outer")
    public ServerResponse outerInventory(@RequestBody Outer outer) {
        Integer res = inventoryService.outer(outer);
        if (res > 0) {
            return ServerResponse.ok("出库成功,出库了" + res + "个货物");
        } else {
            return ServerResponse.badRequest("出库失败");
        }

    }

    @GetMapping("/getLog")
    public ServerResponse getLog() {
        List<Enter> enterRes = inventoryService.queryEnter();
        List<Outer> outerRes = inventoryService.queryOuter();
        HashMap<String, Object> res = new HashMap<>();
        res.put("enter", enterRes);
        res.put("outer", outerRes);
        return ServerResponse.ok(res);
    }
}
